package com.itheima02;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class ReceiveUDPDemo {
    public static void main(String[] args) throws IOException {
        // 创建接收端 socket
        DatagramSocket ds = new DatagramSocket(10086);

        // 创建接收包
        byte[] bytes = new byte[1024];
        DatagramPacket dp = new DatagramPacket(bytes, bytes.length);

        // 接收数据
        ds.receive(dp);

        // 解析数据
        byte[] data = dp.getData();
        int length = dp.getLength();
        String s = new String(data, 0, length);
        System.out.println("接收到的数据是：" + s);

        // 关闭
        ds.close();
    }
}
